package com.mxx.qh.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.mxx.qh.R;
import com.mxx.qh.SPUtils;
import com.mxx.qh.config.ConstantsConfig;
import com.mxx.qh.config.NetUrlConfig;
import com.mxx.qh.interfaces.ILoading;
import com.mxx.qh.utils.PackageUtils;
import com.mxx.qh.utils.WebViewUtils;
import com.mxx.qh.widget.LollipopFixedWebView;
import com.yp.loading.widget.LoadingDialog;

public class BannerHandlerActivity extends BaseActivity {
    private LollipopFixedWebView webView;
    private RelativeLayout fl_video;
    private ImageView iv_background;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    //  横屏时，显示视频的view
    private View mCustomView;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_banner_handler;
    }

    @Override
    protected void setStatusBar() {
        ImmersionBar
                .with(this)
                .fitsSystemWindows(true)
                .statusBarColor(android.R.color.transparent)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initView() {
        fl_video = findViewById(R.id.fl_video);
        iv_background = findViewById(R.id.iv_background);

        fl_video.getBackground().mutate().setAlpha(255);
        initWebView();
    }

    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    private void initWebView() {
        webView = findViewById(R.id.web_view);
        WebViewUtils.initWebView(this,
                webView, new WebViewUtils.UrlHandlerListener() {
                    @Override
                    public void handlerUrl(String url) {
                        Log.i("TAG", url);
                    }

                    @Override
                    public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
                        //如果view 已经存在，则隐藏
                        if (mCustomView != null) {
                            callback.onCustomViewHidden();
                            return;
                        }
                        view.setBackgroundColor(getResources().getColor(R.color.white));
                        mCustomView = view;
                        mCustomView.setVisibility(View.VISIBLE);
                        mCustomViewCallback = callback;
                        fl_video.addView(mCustomView);
                        fl_video.setVisibility(View.VISIBLE);
                        fl_video.bringToFront();

                        //设置横屏
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


                    }

                    @Override
                    public void onHideCustomView() {
                        if (mCustomView == null) {
                            return;
                        }
                        mCustomView.setVisibility(View.GONE);
                        fl_video.removeView(mCustomView);
                        mCustomView = null;
                        fl_video.setVisibility(View.GONE);
                        try {
                            mCustomViewCallback.onCustomViewHidden();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
                    }
                });
    }

    /**
     * 横竖屏切换监听
     */
    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
        switch (config.orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
                break;
            case Configuration.ORIENTATION_SQUARE:
                break;
            case Configuration.ORIENTATION_UNDEFINED:
                break;
        }
    }

    @Override
    protected void initData() {
        String url = getWebViewUrl();
        if (url == null || url.isEmpty()) {
            return;
        }
        webView.loadUrl(url);
    }

    @Override
    protected void initListener() {
        super.initListener();
        webView.goBack();
    }

    private String getWebViewUrl() {
        Intent mIntent = getIntent();
        if (mIntent == null) {
            return null;
        }
        Bundle mBundle = mIntent.getExtras();
        if (mBundle == null) {
            return null;
        }
        return (String) mBundle.getSerializable(ConstantsConfig.VALUE);
    }
}